package cn.kgc.javaweb.service.impl;


import cn.kgc.javaweb.dao.CarChargeDao;
import cn.kgc.javaweb.dao.impl.CarChargeDaoImpl;
import cn.kgc.javaweb.dto.UserPageDTO;
import cn.kgc.javaweb.entity.CarChargeInfo;
import cn.kgc.javaweb.enums.CodeMsg;
import cn.kgc.javaweb.service.CarChargeService;
import cn.kgc.javaweb.util.ResponseResult;

import java.util.List;

public class CarChargeServiceImpl implements CarChargeService {
    CarChargeDao carChargeDao = new CarChargeDaoImpl();

    @Override
    public ResponseResult selectPage(UserPageDTO userPageDTO) {
        ResponseResult responseResult = new ResponseResult();
        try {
            List<CarChargeInfo> carChargeInfos = carChargeDao.selectPage(userPageDTO);
            Long total = carChargeDao.selectTotal(userPageDTO);

            responseResult.setCode(CodeMsg.SEARCH_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.SEARCH_SUCCESS.getMsg());
            responseResult.setCount(total);
            responseResult.setData(carChargeInfos);
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.SEARCH_FAIL.getCode());
            responseResult.setMsg(CodeMsg.SEARCH_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public ResponseResult delete(String ids) {
        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            carChargeDao.delete(ids);
            responseResult.setCode(CodeMsg.DELETE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.DELETE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.DELETE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.DELETE_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public ResponseResult update(CarChargeInfo carChargeInfo) {
        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            carChargeDao.update(carChargeInfo);
            responseResult.setCode(CodeMsg.UPDATE_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.UPDATE_FAIL.getCode());
            responseResult.setMsg(CodeMsg.UPDATE_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public ResponseResult add(CarChargeInfo carChargeInfo) {
        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            carChargeDao.add(carChargeInfo);
            responseResult.setCode(CodeMsg.ADD_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.ADD_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.ADD_FAIL.getCode());
            responseResult.setMsg(CodeMsg.ADD_FAIL.getMsg());
        }
        return responseResult;
    }

    @Override
    public ResponseResult selectAdminById(Integer id) {
        ResponseResult responseResult = ResponseResult.builder().build();
        try {
            CarChargeInfo carChargeInfo =  carChargeDao.selectById(id);
            responseResult.setData(carChargeInfo);
            responseResult.setCode(CodeMsg.SELECT_SUCCESS.getCode());
            responseResult.setMsg(CodeMsg.SELECT_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            responseResult.setCode(CodeMsg.SELECT_FAIL.getCode());
            responseResult.setMsg(CodeMsg.SELECT_FAIL.getMsg());
        }
        return responseResult;
    }
    @Override
    public Boolean selectByCarName(Object carnum) {
        CarChargeInfo carChargeInfo = carChargeDao.selectByCarName(carnum);
        return carChargeInfo != null;
    }
}
